<?php
//By Bing Hua
include_once 'functions.php';
include_once 'insert_functions.php';
$date1 = $_POST ['date1'];
$date2 = $_POST ['date2'];
$date3 = $_POST ['date3'];
$cropNum1 = $_POST ['cropNum1'];
$cropNum2 = $_POST ['cropNum2'];
$cropNum3 = $_POST ['cropNum3'];
$cropName = $_POST ['cropName'];
$cropid = getCropId ( $cropName );
$ok = true;
$msg = "";

if ($cropNum1 != 0) {
	if (checkSeedPurchase ( $cropid, $date1 ) == true) {
		$ok = false;
		$msg = $msg . "Overwrite existing Seeds Bought Data on " . $cropName . "! Quantity is " . $cropNum1 . " and Date is " . $date1 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Seeds Bought Data on " . $cropName . "! Quantity is " . $cropNum1 . " and Date is " . $date1 . "." . '\n' . '\n';
	}
}
if ($cropNum2 != 0) {
	if (checkPlantAdded ( $cropid, $date2 ) == true) {
		$ok = false;
		$msg = $msg . "Overwrite existing Planting Data on " . $cropName . "! Quantity is " . $cropNum2 . " and Date is " . $date2 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Planting Data on " . $cropName . "! Quantity is " . $cropNum2 . " and Date is " . $date2 . "." . '\n' . '\n';
	}
}
if ($cropNum3 != 0) {
	if (checkPlantsTransplanted ( $cropid, $date3 ) == true) {
		$ok = false;
		$msg = $msg . "Overwrite existing Transplanting Data on " . $cropName . "! Quantity is " . $cropNum3 . " and Date is " . $date3 . "." . '\n' . '\n';
	} else {
		$msg = $msg . "Add new Transplanting Data on " . $cropName . "! Quantity is " . $cropNum3 . " and Date is " . $date3 . "." . '\n' . '\n';
	}
}
if ($ok == true) {
	if ($cropNum1 != 0)
		addSeedPurchase ( $cropid, $cropNum1, $date1, null );
	if ($cropNum2 != 0)
		addPlantAdded ( $cropid, $cropNum2, $date2, null );
	if ($cropNum3 != 0)
		addPlantsTransplanted ( $cropid, $cropNum3, $date3, null );
	echo "<script type='text/javascript'>alert('";
	echo $msg;
	echo "')</script>";
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=insert_cropinfo.php">';
} else {
	echo '<script language="JavaScript">';
	$msg = $msg . 'Are you sure?' . '\n';
	echo 'if(confirm(\'' . $msg . '\'))
		location.href = "insert_cropinfo_process_sure.php?date1=' . $date1 . '&date2=' . $date2 . '&date3=' . $date3 . '&cropNum1=' . $cropNum1 . '&cropNum2=' . $cropNum2 . '&cropNum3=' . $cropNum3 . '&cropName=' . $cropName . '"; else
		location.href = "insert_cropinfo.php?date1=' . $date1 . '&date2=' . $date2 . '&date3=' . $date3 . '&cropNum1=' . $cropNum1 . '&cropNum2=' . $cropNum2 . '&cropNum3=' . $cropNum3 . '&cropName=' . $cropName . '";
		</script>';
}
?>